<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 3px 3px 3px 3px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">客人名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="customerName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">工厂名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="supplierName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-refresh-btn"><i class="layui-icon layui-icon-refresh"></i> 刷新
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">
                    添加
                </button>
                limit: 5,
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="uploadFile">
                    上传模板
                </button>
<!--                limit: 5,-->
<!--                <button type="submit" class="layui-btn  layui-btn-sm layui-btn-sm data-add-btn"  lay-event="importDemo">-->
<!--                    下载模板-->
<!--                </button>-->
            </div>
        </script>
        <table class="layui-hide" id="exportTableId" lay-filter="exportTableFilter"></table>
        <script type="text/html" id="exportTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="/static/js/common.js"></script>
<script>
    var lay, form, table;
    layui.use(['form', 'table'], function () {
        lay = layui.jquery;
        form = layui.form;
        table = layui.table;

        table.render({
            elem: '#exportTableId',
            url: '/exportModelConfig/pages',
            toolbar: '#toolbarDemo',
            method: "post",
            request: {
                pageName: "pageNum",
                limitName: "pageSize"
            },
            contentType: "application/json; charset=utf-8",
            where: {
                param: {}
            },
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 65, title: '编号'},
                {field: 'remark', width: 150, title: '名称'},
                {field: 'customerName', width: 150, title: '客人名称'},
                {field: 'supplierName', width: 120, title: '工厂名称'},
                {field: 'type', width: 100, title: '导出类型'},
                {
                    field: 'url', width: 150, title: '模板', templet: function (a, e) {
                        if (a.url) {
                            return '<a style="color: #EC0634" target="_blank" href="' + a.url + '">下载模板</a>';
                        }
                        return '请上传模板';
                    }
                },
                {field: 'status', width: 180, title: '导出数据层级', templet: function (a, e) {
                        if (a.status==1) {
                            return '主表';
                        }
                        if (a.status==2) {
                            return '子表';
                        }
                        if (a.status==3) {
                            return '主子表';
                        }
                        return '';
                    }
                },
                {
                    field: 'createTime',
                    width: 120,
                    title: '创建时间',
                    templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}</div>"
                },
                {field: 'updateUser', width: 100, title: '修改人'},
                {
                    field: 'updateTime',
                    width: 120,
                    title: '修改时间',
                    templet: "<div>{{layui.util.toDateString(d.updateTime, 'yyyy-MM-dd')}}</div>"
                },
                {title: '操作', minWidth: 150, toolbar: '#exportTableBar', align: "center",fixed: 'right'}
            ]],
            done: function (res, curr, count) {  //隐藏列
                if ($.common.userIsAdmin()) {
                    $("[data-field='updateUser']").css('display', 'none');
                    $("[data-field='updateTime']").css('display', 'none');
                }
            },
            limits: [5,10, 15, 20, 25, 50, 100, 500],
            limit: 10,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            search(data);
            return false;
        });

        form.on('submit(data-refresh-btn)', function (data) {
            // 提交刷新了当前页面
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(exportTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加模板',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/export/add.html',
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            }else if (obj.event === 'uploadFile') {
                console.log(111111)
                let checkStatus = table.checkStatus('exportTableId');
                if (checkStatus.data.length != 1) {
                    $.common.showErrorMessage("请选择一条数据");
                    return false;
                }
                let id = checkStatus.data[0].id;
                var index = layer.open({
                    title: '上传模板',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '95%'],
                    content: '/static/page/export/uploadFile.html?id=' + id,
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'importDemo') {
                let checkStatus = table.checkStatus('exportTableId');
                if (checkStatus.data.length != 1) {
                    $.common.showErrorMessage("请选择一条数据");
                    return false;
                }
                let url = checkStatus.data[0].url;
                if(url){
                    window.location.href = url;
                }else{
                    $.common.showErrorMessage("该数据暂未上传模板，无法下载");
                    return false;
                }

            }
        });

        //监听表格复选框选择
        table.on('checkbox(exportTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(exportTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '修改',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/export/add.html?id=' + data.id
                        + "&supplierId=" + data.supplierId + "&customerId=" + data.customerId+ "&status=" + data.status,

                    data: data,
                    success: function (index, layero) {
                        var body = layer.getChildFrame('body', layero);
                        for (let key in data) {
                            body.find("#" + key).val(data[key]);
                        }
                    },
                    end: function () {

                    }
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除？', function (index) {
                    let url = "/exportModelConfig/delete/" + obj.data.id;
                    let callback = {
                        onSuccess: function (result) {
                            if (result.code == 0) {
                                window.location.reload();
                            } else {
                                $.common.showErrorMessage(result.message)
                                return false;
                            }
                        }
                    };
                    $.xAjax(url, 'POST', JSON.stringify(data.field), callback);
                });
            }
        });

    });

    function search(data) {
        table.reload('exportTableId', {
            page: {
                curr: 1
            }
            , where: {
                param: data.field
            }
        }, 'data');
    }
</script>

</body>
</html>
